﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Linq;
using FeeWcfService.DataContracts;

namespace FeeWcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class FeeService : IFeeService
    {
        public List<PriceCutOff> GetPriceCutOffs(Guid portfolioGuid, DateTime valueAsOf)
        {

            XDocument xdoc = XDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_data\\PriceCutOff.xml"));

            var priceCutOffs = (from priceCiutOff in xdoc.Descendants("PriceCutOff")
                                select new PriceCutOff
                                {
                                    Id = Int32.Parse(priceCiutOff.Element("ID").Value),
                                    Label = priceCiutOff.Element("Label").Value,

                                    ReportingAsOf = DateTime.Parse(priceCiutOff.Element("ReportingAsOf").Value),

                                    ValueAsOf = DateTime.Parse(priceCiutOff.Element("ValueAsOf").Value)
                                }).ToList();
            return priceCutOffs;
        }


        public List<Portfolio> GetPortfolios()
        {

            System.Threading.Thread.Sleep(5000);
            XDocument xdoc = XDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_data\\Portfolos.xml"));

            var portfolios = (from portfolio in xdoc.Descendants("Portfolio")

                              select new Portfolio
                              {
                                  Currency = portfolio.Element("Currency").Value,
                                  ExchangeRateSet = Int16.Parse(portfolio.Element("ExchangeRateSet").Value),

                                  Name = portfolio.Element("Name").Value,
                                  Pfo = (int)((float.Parse(portfolio.Element("Pfo").Value)) * 1000),
                                  PortfolioGuid = new Guid(portfolio.Element("PortfolioGuid").Value),
                                  ValCalendar = Int16.Parse(portfolio.Element("ValCalendar").Value),

                              }).ToList();
            return portfolios;
        }



        public DataResult GetPortfolioExcel(Portfolio portfolio, DateTime date, PriceCutOff priceCutOff)
        {

            System.Threading.Thread.Sleep(3800);
            byte[] fileBytes = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_data\\spreadsheet.bin"));


            return new DataResult
            {
                Data = fileBytes,
                IsSuccess = true,
                Message = "message"
            };

        }
    }
}
